package com.zhangyuji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangyuji.common.MyCurrentId;
import com.zhangyuji.common.R;
import com.zhangyuji.entity.Employee;
import com.zhangyuji.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee , HttpServletRequest request){

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());

        Employee queryEmployee = employeeService.getOne(queryWrapper);

        if (queryEmployee == null){
            return R.error("没有此账号");
        }

        if (!password.equals(queryEmployee.getPassword())){
            return R.error("密码错误");
        }

        if (queryEmployee.getStatus() == 0){
            return R.error("此员工已被禁用");
        }

        request.getSession().setAttribute("employee",queryEmployee.getId());
        return R.success(queryEmployee);
    }


    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("成功退出账号");
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee,HttpServletRequest request){
        System.out.println(employee);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        Long employeeId = (Long) request.getSession().getAttribute("employee");

        MyCurrentId.setCurrentId(employeeId);
//        employee.setCreateUser(employeeId);
//        employee.setUpdateUser(employeeId);
        System.out.println(employee);
        boolean flag = employeeService.save(employee);
       if (flag){
           return R.success("添加成功");
       }
        return R.error("添加失败");
    }


    @GetMapping("/page")
    public R<IPage> page(int page,int pageSize,String name){
        System.out.println(page);
        System.out.println(pageSize);
        System.out.println(name);
        IPage<Employee> iPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Employee::getName,name);

        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(iPage,queryWrapper);


        return R.success(iPage);
    }

    @PutMapping
    public R<String> editEmployee(@RequestBody Employee employee,HttpServletRequest request){
        System.out.println(employee);
        Long adminId = (Long) request.getSession().getAttribute("employee");
        System.out.println(adminId);
        if (adminId.equals(employee.getId())){
            return R.error("不能修改管理员权限");
        }
        MyCurrentId.setCurrentId(adminId);
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(adminId);
        boolean flag = employeeService.updateById(employee);

        System.out.println(flag);

        if (flag){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @GetMapping("/{id}")
    public R<Employee> queryEmployeeById(@PathVariable Long id ){
        log.info("当前需要修改的id为:{}",id);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getId,id);
        Employee queryEmployee = employeeService.getOne(queryWrapper);
        log.info(queryEmployee.toString());
        if (queryEmployee != null){
            queryEmployee.setPassword("");
            log.info(queryEmployee.toString());
            return R.success(queryEmployee);
        }

        return R.error("查询失败");
    }


}
